#ifndef SERIAL_CONNECTION_HPP
#define SERIAL_CONNECTION_HPP

#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termio.h>
#include <string>

using namespace std;

#define clear(var, mask)      var &= (~(mask))
#define set(var,mask)         var |= (mask)

class Serial {

  public:

     //Purpose:         Constructor.
     //Preconditions:   None.
     //PostConditions:  Sets Verbose to bool value, message size,
     //                   and default timeout and retry.
     Serial(bool);

     //Purpose:         Open connection to Khepera on port passed
     //                 as a parameter e.g., "ttya".
     //Preconditions:   Port name is defined
     //Postconditions:  Connection open to given port. Returns true if successful,
     //                 false otherwise.
     bool Open(std::string portname);

     //Purpose:         Send command to Khepera and return response.
     //Preconditions:   Commands are in the form of a capital letter,
     //                   followed by any parameters needed, ending with
     //                   a carriage return and line feed.
     //Postconditions:  Responses are in the form of a lowercase letter
     //                 corresponding to the capital letter command, then
     //                 any data it needs to transmit and a carriage return.
     std::string Talk(std::string send, int size = 64);

     //Purpose:         Close the serial connection.
     //Preconditions:   Connection must be open.
     //Postconditions:  Connection is closed to port. Returns true if successful,
     //                 false otherwise.
     bool Close();


  private:
     bool VERBOSE;
     int MESSAGE_SIZE;
     int DEFAULT_TIMEOUT;
     int DEFAULT_RETRY;
     int SERIAL_ID;
     std::string Readline(int size = 64);
     bool Configure();
     bool Drain();


};

#endif
